package com.example.foodiemoments.ui.fragments

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.example.foodiemoments.LoginActivity
import com.example.foodiemoments.databinding.FragmentProfileBinding

class ProfileFragment : Fragment() {

    private var _binding: FragmentProfileBinding? = null
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentProfileBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setupViews()
        loadUserData()
    }

    private fun setupViews() {
        // 编辑资料
        binding.btnEditProfile.setOnClickListener {
            // TODO: 跳转到编辑资料页面
        }

        // 我的作品
        binding.layoutMyRecipes.setOnClickListener {
            // TODO: 跳转到我的作品页面
        }

        // 我的收藏
        binding.layoutMyCollections.setOnClickListener {
            // TODO: 跳转到我的收藏页面
        }

        // 我的关注
        binding.layoutMyFollowing.setOnClickListener {
            // TODO: 跳转到我的关注页面
        }

        // 粉丝
        binding.layoutMyFans.setOnClickListener {
            // TODO: 跳转到粉丝列表页面
        }

        // 设置
        binding.layoutSettings.setOnClickListener {
            // TODO: 跳转到设置页面
        }

        // 关于
        binding.layoutAbout.setOnClickListener {
            // TODO: 跳转到关于页面
        }

        // 退出登录
        binding.btnLogout.setOnClickListener {
            logout()
        }
    }

    private fun loadUserData() {
        // TODO: 从数据库或网络加载用户数据
        binding.apply {
            tvUsername.text = "美食达人小王"
            tvSignature.text = "热爱美食，享受生活 🍜"
            tvRecipeCount.text = "23"
            tvFollowingCount.text = "156"
            tvFansCount.text = "892"
        }
    }

    private fun logout() {
        // TODO: 清除用户数据
        startActivity(Intent(requireContext(), LoginActivity::class.java))
        requireActivity().finish()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}
